package com.gxa.pingan.controller;

import com.gxa.pingan.common.JsonResult;
import com.gxa.pingan.pojo.ETC;
import com.gxa.pingan.service.ETCService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * ETC的接口
 *
 * @author lijianmin
 * @date 2021/10/25  19:03:57
 */
@CrossOrigin
@RestController
@RequestMapping("/etc")
@Api(value = "ETC申请的接口", tags = "ETC申请接口的API")
public class ETController {

    @Autowired
    private ETCService eTCService;

    /**
     * ETC申请列表查询的方法,搜索条件为空则进行分页查询,搜索条件不为空则进行条件查询+分页查询
     *
     * @param name 搜索条件
     * @param page 页码
     * @param size 每页记录数
     * @return 返回查询到的集合
     */
    @GetMapping("/getList")
    @ApiOperation(value = "ETC申请列表查询的方法,搜索条件为空则进行分页查询,搜索条件不为空则进行条件查询+分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "搜索条件", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "size", value = "每页记录数", paramType = "query", required = true, dataType = "Integer")
    })
    public Map getList(String name, @RequestParam("page") Integer page, @RequestParam("size") Integer size) throws Exception {
        List<ETC> list = eTCService.getList(name, page, size);
        Integer listCount = eTCService.getListCount(name);

        JsonResult jsonResult = new JsonResult();
        if (listCount != 0) {
            jsonResult.setData(list);
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.put("count", listCount);
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("当前无数据");
        }
        return jsonResult.getDataMap();
    }


    @PostMapping("export")
    @ApiOperation(value = "导出全部数据的方法")
    public void export(HttpServletResponse response) throws Exception {
        // public ResponseEntity<byte[]> export() throws Exception {
        /*List<ETC> list = eTCService.getList(null, null, null);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        EasyExcel.write(outputStream, ETC.class).sheet("ETC申请表").doWrite(list);

        byte[] bytes = outputStream.toByteArray();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment", URLEncoder.encode("ETC申请表.xlsx", "utf-8"));

        return new ResponseEntity<>(bytes, httpHeaders, HttpStatus.OK);*/


        List<ETC> all = eTCService.getList(null, null, null);

        // 工作簿对象(excel文件对象)
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建工作表
        HSSFSheet sheet = workbook.createSheet("ETC申请列表");
        String fileName = "ETC申请列表.xls";
        // 将文件名中的中文部分被URL编码后的内容进行解析, 最终还是显示为中文状态
        fileName = URLEncoder.encode(fileName, "utf-8").replaceAll("\\+", "%20");
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = {"编号", "姓名", "手机号", "车牌号", "是否是京籍", "地址", "申请时间", "银行名称"};
        // 创建一个行对象, 作为表头
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            // 在行中添加单元格
            HSSFCell cell = row.createCell(i);
            //先把文字转换为标准的富文本格式
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            // 把转换后的字段名添加到表头行上
            cell.setCellValue(text);
        }

        //准备将时间格式处理为标准日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 添加数据行:
        int rowNum = 1;
        for (ETC item : all) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(item.getId());
            dataRow.createCell(1).setCellValue(item.getName());
            dataRow.createCell(2).setCellValue(item.getPhone());
            dataRow.createCell(3).setCellValue(item.getCarNum());
            dataRow.createCell(4).setCellValue(item.getIsJing());
            dataRow.createCell(5).setCellValue(item.getAddress());
            dataRow.createCell(6).setCellValue(dateFormat.format(item.getApplyTime()));
            dataRow.createCell(7).setCellValue(item.getBankName());
            rowNum++;
        }

        //返回给前端下载
        response.setContentType("application/octet-stream");
        //告诉浏览器, 以附件方式打开,而不是页面上浏览
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

}
